<?php
	include_once ( "AbstractDAO.php" );
	
	class ProductDAO extends AbstractDAO 
	{
	    function GetProductInfo($pid)
		{
			$sql="select * from product_info where status<>9 and id in(".$pid . ")";
			return $this->db->GetRow($sql);
		}
		
		function GetProductInfo2($pid)
		{
			$sql="select p.product_name,p.shop_id,p.cate_id,p.pic_path,p.price,p.location,p.product_url,p.description,p.create_user_id,p.modify_user_id,
			      p.first_user_id,p.score,p.reality,p.service,p.velocity,p.rate,p.review_count,p.view_count,p.create_time,p.modify_time,p.first_time,
			      s.paymethod,s.feature,s.tag,s.shop_name,s.plat_id, p.status 
                  from product_info p,shop_info s where p.status<>9 and s.id=p.shop_id and p.id=".$pid;
			return $this->db->GetRow($sql);
		}
		
		function GetProductInfoPlus($platform, $pid)
		{		
			$sql="select * from product_info_".$platform." where product_id=".$pid;		
			return $this->db->GetRow($sql);	
		}	

		function GetProductInfoPlusByExtID($platform, $extID)
		{		
			$sql="select pi.id,pi.product_name, pip.".$platform."_id from product_info pi , product_info_".$platform." pip where pi.id = pip.product_id and pip.".$platform."_id=".$extID;
	
			return $this->db->GetRow($sql);	
		}			
		
		function GetProductName($pid)
		{
			return $this->db->GetValue("select product_name from product_info where id=".$pid);
		}

	
		function GetProductBySid($sid, $pcid='0', $order='review_count', $perpage='20', $cpage='1', $sort='desc')
		{
			$result=array();
			$sql="select count(*) from product_info where status<>9 and shop_id=".$sid;
			if($pcid!='0')
			{
				$sql=$sql." and cate_id=".$pcid;
			} 
//			if($keyword!='')转到搜索页面
//			{
//				$sql=$sql." and product_name like '%".$keyword."%'";
//			}
			$count=$this->db->GetValue($sql);
			$result['total_num']=$count;
			if($count!='0')
			{
				$sql="select id, product_name, cate_id, pic_path, price, review_count, create_time from product_info 
				where status<>9 and shop_id=".$sid;
				if($pcid!='0') 
				{
					$sql=$sql." and cate_id=".$pcid;
				} 
//				if($keyword!='')转到搜索页面
//				{
//					$sql=$sql." and product_name like '%".$keyword."%'";
//				}
				$sql=$sql.' order by '.$order.' '.$sort;

				$result['res']=$this->getRows($sql, $perpage, $cpage);

			}
			return $result;
		}
		
		
		function GetProductCategoryBySid($sid)
		{
			$query_res=$this->db->GetPlan("select distinct(product_info.cate_id), product_category.cate_name from product_info, 
		                                 	product_category where product_info.status<>9 and product_info.shop_id=".$sid." and product_info.cate_id=product_category.id");
			if(!$query_res)
				return false;
				
			$result=array();
			
			foreach($query_res as $category)
			{
				$result[$category['cate_name']]=$category['cate_id'];
			}
			
			return $result;
		}
		
		function UpdateProductViewCount($pid)
		{
			$sql = "update product_info set view_count = view_count +1 where id=".$pid;
			$this->db->Update($sql);				
	    }
			
		/**
		 * add_product
		 */				
		function GetProductIDByURL($productURL,$platform='')
		{
			 if(!edp_is_empty_string($platform))
			   return $this->db->GetValue("select product_id from product_info_".$platform." where product_url='$productURL'");
		     else
			   return $this->db->GetValue("select id from product_info where product_url='$productURL'");
		}	
			
		function GetProductIDByExtID($extProductID,$platform)
	    { 	
	    	$sql = "select product_id from product_info_".$platform." where ".$platform."_id='$extProductID'";
		 
		    return $this->db->GetValue($sql);
    	}
	
		function InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID)
		{	
			$now=GetCurrentDate();
						  	
			$sql = "insert into 
					product_info( cate_id, shop_id, product_name, product_url, pic_path, location, price, description,
									 status, create_user_id, create_time) 
					values( $pcid, $sid, '$productName', '$productURL', '$productPicture', '$productLocation', '$productPrice', '$productDesc',
							$status, $userID, '$now') ";
			$this->db->Insert( $sql );		
			$id = $this->db->LastID();	
			return $id;
		}
				
		
		function InsertTaobaoProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID, $extProductID, $click_url, $commission_rate)
		{
			try
	    	{
	    		$this->db->BeginTransaction();						  
				$id = $this->InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
									  $productPrice,$productDesc,$status,$userID);
						
				$sql = " insert into product_info_taobao values( $id, '$extProductID', '$click_url', '$commission_rate' ) ";
				$this->db->Insert( $sql );
			}		
			catch( Exception $e)
	    	{
	    		
	    		$this->db->Rollback();
	    		$id='';
	    		return false;
	    	}
	    	$this->db->Commit();
			return 	$id;				
							  								  	
		}
		
		function InsertEachnetProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID, $extProductID)
		{

			try
	    	{
	    		$this->db->BeginTransaction();
				$id = $this->InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
									  $productPrice,$productDesc,$status,$userID);
						
				$sql = " insert into product_info_eachnet values( $id, '$extProductID' ) ";
				$this->db->Insert( $sql );
			}		
			catch( Exception $e)
	    	{
	    		$this->db->Rollback();
	    		$id='';
	    		return false;
	    	}
	    	$this->db->Commit();
			return 	$id;								  								  	
		}
		
		function InsertPaipaiProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID, $extProductID)
		{
			try
	    	{
	    		$this->db->BeginTransaction();
				$id = $this->InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
									  $productPrice,$productDesc,$status,$userID);
						
				$sql = " insert into product_info_paipai values( $id, '$extProductID' ) ";
				$this->db->Insert( $sql );
			}		
			catch( Exception $e)
	    	{
	    		$this->db->Rollback();
	    		$id='';
	    		return false;
	    	}
	    	$this->db->Commit();
			return 	$id;								  								  	
		}
		
		function InsertYouaProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID, $extProductID)
		{
			try
	    	{
	    		$this->db->BeginTransaction();					  
				$id = $this->InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
									  $productPrice,$productDesc,$status,$userID);
						
				$sql = " insert into product_info_youa values( $id, '$extProductID' ) ";
				$this->db->Insert( $sql );
			}		
			catch( Exception $e)
	    	{
	    		$this->db->Rollback();
	    		$id='';
	    		return false;
	    	}
	    	$this->db->Commit();
			return 	$id;								  								  	
		}

		function InsertB2CProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID)
		{
			try
	    	{
	    		$this->db->BeginTransaction();
				$id = $this->InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
									  $productPrice,$productDesc,$status,$userID);
						
				$sql = " insert into product_info_b2c values( $id,'$productURL' ) ";			
				$this->db->Insert( $sql );		
			}		
			catch( Exception $e)
	    	{
	    		$this->db->Rollback();
	    		$id='';
	    		return false;
	    	}
	    	$this->db->Commit();
			return 	$id;	
		}

		function InsertOtherB2CProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
								  $productPrice,$productDesc,$status,$userID,$extProductID,$plat_code)
		{
			try
	    	{
	    		$this->db->BeginTransaction();
				$id = $this->InsertCommonProductData($pcid,$sid,$productName,$productURL,$productPicture,$productLocation,
									  $productPrice,$productDesc,$status,$userID);
						
				$sql = " insert into product_info_".$plat_code." values( $id,'$extProductID' ) ";			
				$this->db->Insert( $sql );		
			}		
			catch( Exception $e)
	    	{
	    		$this->db->Rollback();
	    		$id='';
	    		return false;
	    	}
	    	$this->db->Commit();
			return 	$id;	
		}				
		
		/**
		 * get product info list by ids array
		 *
		 * @param array $ids
		 * @return array
		 */
		function GetProductInfoByIdList( $ids ){
			$strIds = implode( ",", $ids );

			//!!!!shit here, need to sort the result with extra sql statement : ORDER BY FIELD(id, ".$strIds.")
			//and then do sort here
			$result = $this->db->GetPlan( "select *,
					(select shop_name from shop_info where id=p.shop_id) as shop_name,
					(select plat_id from shop_info where id=p.shop_id) as plat_id,
					(select name from citylist where id=p.location) as location_text,
					(select cate_name from product_category where id=p.cate_id) as cate_name
					 from product_info p where p.status<>9 and p.id in (".$strIds.") ORDER BY FIELD(p.id, ".$strIds.")" );
			return $result;
		}
		
		 /**
		 * This is used by product_summary page
		 * 
		 */
		function GetProductInfoByIdList2( $ids ){
			$strIds = implode( ",", $ids );

			//!!!!shit here, need to sort the result with extra sql statement : ORDER BY FIELD(id, ".$strIds.")
			//and then do sort here
			$result = $this->db->GetPlan( "select id, product_name, cate_id, pic_path, price, review_count, create_time,
					(select cate_name from product_category where id=p.cate_id) as cate_name
					 from product_info p where p.status<>9 and p.id in (".$strIds.") ORDER BY FIELD(p.id, ".$strIds.")" );
			return $result;
		}
		
		
		//Used by review_info
		function GetProductByIdList($ids)
		{
			return $result=$this->db->GetPlan("select id, product_name from product_info where status<>9 and id in (".$ids.")");
		}
		
		/**********************************************************
		 *	Used for the product search in the user's list
		 *	Seal Created by 2010-1-31
		 **********************************************************/
		function GetProductSimpleInfo( $ids )
		{
			if( count($ids) == 0 )
				return false;
				
			$strIds = implode( ",", $ids );
			
			$sql = "select p.id, p.product_name,
						(select shop_name from shop_info where id=p.shop_id) as shop_name,
						(select nick from shop_info where id=p.shop_id) as nick,										
						(select plat_id from shop_info where id = p.shop_id) as plat_id,
						(select plat_name from shop_platform where id=plat_id) as plat_name,								
						p.score,p.reality, p.service, p.rate, p.velocity, p.pic_path
				  	from product_info p
				 	where p.status<>9 and p.id in ( $strIds ) ";
			
			$result = $this->db->GetPlan( $sql );
			
			return $result;
		}
		
		function GetRecentAddedProductIds( $shop_id, $keyword )
		{
			$time = EdpUnixTimeStampToMySQL( time() - 5 * 60 );
			$sql = "select id
			   		  from product_info 
			   		 where status <> 9
			   		   and shop_id = $shop_id 
			   		   and create_time >= '$time' 
			   		   and product_name like '%$keyword%' ";
			return $this->db->GetPlan( $sql );	
		}
		
		function GetProductForValidation( $ids )
		{
			$sql = " select id, pic_path
					 from product_info p
					 where status<>9 and id in ($ids)";
			
			return $this->db->GetPlan( $sql );
		}
		
		function GetProductByUid($uid, $count='20')
		{
			$sql="select * from product_info where status<>9 and create_user_id=".$uid." order by id desc limit 0,".$count;
			return $this->db->GetPlan($sql);
		}
		
		function UpdateFirstReviewer($pid_list, $uid, $date)
		{
			if(edp_is_empty_string($pid_list)){
				
				return false;
			
			}else {
				
				$sql="select id from product_info where status<>9 and id in ($pid_list) and review_count=1";////add review之后调用，所以为1
				$result=$this->db->GetCol($sql);
				if($result==false)
					return false;
				else
				{
					$id_list=implode(',',$result);
					$sql="update product_info set first_user_id=$uid, first_time='$date' where id in ($id_list)";
					$this->db->Update( $sql );
					return true;
				}
			}	
		}
		
		function UpdateProductViewHistory($userip,$pid)
		{
			//$nowday = date("Y-m-d");	
		 	$now=GetCurrentDate();
		 	$status = $this->db->GetValue("select * from product_view_history where user_ip = ". ip2long($userip) . " and product_id = $pid"); 	
		 	if($status==false)
		 	{
			 	$sql = "insert into 
						product_view_history(user_ip, product_id, date) values( " . ip2long($userip) . ", $pid, '" . GetCurrentDate() . "')";	
				$this->db->Insert( $sql );	
			}
			else
			{
			   	$sql = "update product_view_history set date='" . GetCurrentDate() . "' where user_ip = " . ip2long($userip) . " and product_id = $pid";	
				$this->db->Update( $sql );	
			}
		}
		
		function GetHotProduct( $count = 12 )
		{
			
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'hotProduct_'.$count, CNS_PRODUCT_SHOP_VARIABLE );
			
			if( $re!= false )
				return $re;
			
			$searchEngine = GetProductSearch();
			$ids = $searchEngine->GetHotProduct( $count );
			
			if( $ids === false )
				return false;
				
			$sql = "select id, product_name, pic_path, price from product_info where id in(" . implode(',', $ids) . ")";
			
			return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, 'hotProduct_'.$count), $ids );
		
		}
		
		function GetHotProductSameShop($pid, $count = 5 )
		{
			$sid = $this->db->GetValue("select shop_id from product_info where status<>9 and id=".$pid);
			
			if(!$sid)
				return false;
				
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'hotProductBySid_'.$sid.'_'.$count, CNS_PRODUCT_SHOP_VARIABLE );
			
			if( $re!= false )
				return $re;
						
			$searchEngine = GetProductSearch();
			$ids = $searchEngine->GetHotProductSameShop( $sid, $count + 1 );
			
			if( $ids === false )
				return false;

			
			foreach( $ids as $key => $value )
			{
				if( $value == $pid )
				{
					unset( $ids[$key] );
					break;
				}
			}
			if( count($ids) == 0 )
				return false;
			
			if( count($ids) > $count ) 
				array_splice( $ids, $count);

			
				
			$sql = "select id, product_name, pic_path, review_count from product_info where id in(" . implode(',', $ids) . ")";
			
			return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, 'hotProductBySid_'.$sid.'_'.$count), $ids );
			
		}
			
		
		function GetHotProductSameCategory($pid, $count = 5 )
		{
			$pcid = $this->db->GetValue("select cate_id from product_info where id=".$pid);
			
			if(!$pcid)
			 	return false;
			 	
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'hotProductByPcid_'.$pcid.'_'.$count, CNS_PRODUCT_SHOP_VARIABLE );
			
			if( $re!= false )
				return $re;
			
			$searchEngine = GetProductSearch();
			$ids = $searchEngine->GetHotProductSameCategory( $pcid, $count );
			
			if( $ids === false )
				return false;
				
			foreach( $ids as $key => $value )
			{
				if( $value == $pid )
				{
					unset( $ids[$key] );
					break;
				}
			}
			if( count($ids) == 0 )
				return false;
			
			if( count($ids) > $count ) 
				array_splice( $ids, $count);
				
			$sql = "select id, product_name, pic_path, review_count from product_info where id in(" . implode(',', $ids) . ")";
			
			return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, 'hotProductByPcid_'.$pcid.'_'.$count ), $ids );
		}
		

		function GetHotProductBySid( $sid, $count = 8 )
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'hotProductBySid_'.$sid.'_'.$count, CNS_PRODUCT_SHOP_VARIABLE );
			
			if( $re!= false )
				return $re;
					
			$searchEngine = GetProductSearch();
			$ids = $searchEngine->GetHotProductSameShop( $sid, $count );
			
			if( $ids === false )
				return false;
				
			$sql = "select id, product_name, pic_path, review_count from product_info where id in(" . implode(',', $ids) . ")";
			
			return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, 'hotProductBySid_'.$sid.'_'.$count), $ids );
		}
		
		
		function GetHotProductByPlatid( $platid, $count = 5 )
		{
						 	
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'hotProductByPlatid_'.$platid.'_'.$count, CNS_PRODUCT_SHOP_VARIABLE );
			
			if( $re!= false )
				return $re;
			
			$searchEngine = GetProductSearch();
			$ids = $searchEngine->GetHotProductByPlatid( $platid, $count );
			
			if( $ids === false )
				return false;
				
			$sql = "select id, product_name, pic_path, review_count from product_info where id in(" . implode(',', $ids) . ")";
			
			return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, 'hotProductByPlatid_'.$platid.'_'.$count), $ids );
			
		}
				
		function GetProductCountBySid($sid)
		{
			$sql="select count(*) from product_info where status<>9 and shop_id=".$sid;
			return $this->db->GetValue($sql);
		}
		
		
	}

?>
